<?php
namespace wof;

class Cat {
    private $db;
    
    public function __construct() {
        $this->db = new DBConfig;
    }
    
    /* Generate category */
    public function generate_cat() {
        // Get list category
        $o_ListCat = $this->o_fListcategories();
        $i_CatTotal = count((array) $o_ListCat);
        
        // Random select category
        $i_SelectCatId = rand(0, $i_CatTotal - 1);
        $a_SelectCat = $o_ListCat->{$i_SelectCatId};
        // Return cat name
        return $a_SelectCat;
    }
    
    /* Get categories list from DB */
    public function o_fListcategories()
    {
        $o_Categories = new \stdClass();
        $sql="SELECT * FROM wof_cat ORDER BY cat_id ASC";
        $statement = $this->db->pdo->prepare($sql);
        $statement->execute();
        
        $i= 0;
        while ($row = $statement->fetchObject())
        {
            $a_Cat = array();
            $a_Cat['id'] = $row->cat_id;
            $a_Cat['name'] = $row->cat_name;
            $o_Categories->{$i} =$a_Cat;
            $i++;
        }
        
        return $o_Categories;
    }
}

?>